<?php

/**
 * 
 * 分类光光模型模型
 */
class CatadModel extends Model {

    private $article, $link;

    public function _initialize() {
        $this->article = M('PayPage');
        $this->link = D('PayLink');
    }

    public function fabu($param) {
        //参数过滤
        if (!$param['cat_id']) {
            return reStatus(false, '请选择广告分类');
        }
        if (!$param['area_id']) {
            return reStatus(false, '请选择地区');
        }

        M()->startTrans();
        //是否有链接
        if ($param['link_status'] == 1) {
            $link_id = $this->link->newLink($param['link'], $param['link_days']);
            if ($link_id == false) {
                M()->rollback();
                return reStatus(false, '链接发布失败');
            }
        }
        //是否有次级页面
        if ($param['page_status'] == 1) {
            if ($param['page_id']) {
                $payPage = $this->article->where(array('page_id'                  => $param['page_id']))->find();
                $payPage['con_type']       = 'ad';
                $payPage['star_time']      = time();
                $payPage['end_time']       = time() + 24 * 3600 * $param['page_days'];
                $payPage['days']           = $param['page_days'];
                $payPage['article_status'] = 1;
                $pagStatus                 = $this->article->save($payPage);
                if ($pagStatus == false) {
                    M()->rollback();
                    return reStatus(false, '次级页面修改错误');
                }
            } else {
                M()->rollback();
                return reStatus(false, '次级页面发布有误');
            }
        }
        $catad = array(
            'user_id'      => session('uid'),
            'title'        => t($param['title']),
            'cat_id'       => t($param['cat_id']),
            'area_id'      => t($param['area_id']),
            'main_img_url' => t($param['main_img_url']),
            'link_id'      => $param['link_status'] ? $link_id : 0,
            'link_days'    => $param['link_days'],
            'article_id'   => $param['page_id'],
            'article_days' => $param['page_days'],
            'star_time'    => time(),
            'end_time'     => time() + 24 * 3600 * (int) $param['days'],
            'days'         => $param['days'],
            'order_status' => 1,
            'catad_status' => 1,
            'pay_score'    => $param['pay_score'],
            'create_time'  => time(),
        );
        $catadID       = $this->add($catad);
        if ($catadID) {
            M()->commit();
            return reStatus(true, '分类广告发布成功', $catadID);
        } else {
            M()->rollback();
            return reStatus(false, '分类广告发布失败');
        }
    }

}

?>
